package org.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SignOutServlet extends HttpServlet {
    private static final String MAIN = "/WEB-INF/JSP/catalog/Main.jsp";
    private final static String ERROR = "/WEB-INF/JSP/common/Error.jsp";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        if(session.getAttribute("account")==null) {
            session.setAttribute("message","Please Sign In At First!");
            request.getRequestDispatcher(ERROR).forward(request,response);
            return;
        }

        session.removeAttribute("account");
        session.removeAttribute("myList");
        session.removeAttribute("authenticated");
        request.getRequestDispatcher(MAIN).forward(request,response);
    }
}
